using System;
using System.Text;
using InfoJetSoft.Service.XPath.Tokens;

//CID 0164 //2008.12.13 //Begin
namespace InfoJetSoft.Service.XPath.Exprs
{
	/// <summary>
	/// Summary description for AbbrStep.
	/// </summary>
    class AbbrStep : IExpr
	{
		private string _abbr = null;

		public AbbrStep()
		{
			//
			// TODO: Add constructor logic here
			//
        }

        public object Evaluate(ExprContext context)
        {
            //CID 0817 //2011.03.02 //Begin
            if (this._abbr.Equals("."))
            {
                INodeList contextNodes = context.Calculator.GetContextNodes(true);
                return contextNodes;
            }
            else
            {
                INodeList contextNodes = context.Calculator.GetContextNodes(false);
                INodeList parentNodes = context.Calculator.CreateNodeList();
                if (contextNodes.Count > 0)
                {
                    foreach (object node in contextNodes)
                    {
                        object parentNode = context.Calculator.GetParentNode(node, true);
                        if (!parentNodes.HasNode(parentNode))
                        {
                            parentNodes.Append(parentNode);
                        }
                    }
                }
                context.Calculator.Slash(parentNodes);
                return parentNodes;
            }
            //CID 0817 //2011.03.02 //End
        }

        //CID 0866 //2011.04.01 //Begin
        public void Visit(IExprVisitor visitor)
        {
        }
        //CID 0866 //2011.04.01 //End

        public void Print(StringBuilder buffer)
        {
            buffer.Append(this._abbr);
        }

		public static AbbrStep Parse( ExprParser parser )
		{
			Token nextToken = parser.LookupNextToken(1);
			if( nextToken == null )
			{
				return null;
			}
			if( nextToken.Type.Equals( TokenType.Symbol ) )
			{
				if( nextToken.Value.Equals( "." ) || nextToken.Value.Equals( ".." ) )
				{
					parser.MoveNextIndex(1);
					AbbrStep abbrStep = new AbbrStep();
					abbrStep._abbr = nextToken.Value;
					return abbrStep;
				}
			}
			return null;
		}
	}
}
//CID 0164 //2008.12.13 //End
